#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import unittest
from io import BytesIO


class BytesIOTest(unittest.TestCase):
    '''
    读取的也是一个bytes，而不是一个str
    '''

    def test_read(self):
        with BytesIO(b'\xe4\xb8\xad\xe6\x96\x87') as f:
            print(f.read())

    '''
        注意！写入的不是str，而是经过utf8编码的bytes
    '''

    def test_write(self):
        with BytesIO() as f:
            print(f.write('中文'.encode('utf-8')))  # 6
            print(f.getvalue())  # b'\xe4\xb8\xad\xe6\x96\x87'


if __name__ == '__main__':
    unittest.main()
